﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Tanmia.Controls
{
    /// <summary>
    /// Interaction logic for CurrencyTextBox.xaml
    /// </summary>
    public partial class CurrencyTextBox : TextBox
    {
        public static readonly DependencyProperty CurrencyProperty = DependencyProperty.Register("Currency", typeof(string), typeof(CurrencyTextBox), new FrameworkPropertyMetadata(""));

        public string Currency
        {
            get { return (string)GetValue(CurrencyProperty); }
            set { SetValue(CurrencyProperty, value); }
        }

        public CurrencyTextBox()
        {
            InitializeComponent();
        }

        private string reval;

        private string DeleteCommas(string v)
        {
            return v.Replace(",", "");
        }

        private string ApplyCurrencyTemplate(string v)
        {
            string value;
            value = DeleteCommas(v);
            reval = value;

            if (value.Length >= 4)
                reval = reval.Insert(value.Length - 3, ",");
            if (value.Length >= 7)
                reval = reval.Insert(value.Length - 6, ",");
            if (value.Length >= 10)
                reval = reval.Insert(value.Length - 9, ",");
            if (value.Length >= 13)
                reval = reval.Insert(value.Length - 12, ",");
            if (value.Length >= 16)
                reval = reval.Insert(value.Length - 15, ",");
            if (value.Length >= 19)
                reval = reval.Insert(value.Length - 18, ",");
            if (value.Length >= 22)
                reval = reval.Insert(value.Length - 21, ",");
            if (value.Length >= 25)
                reval = reval.Insert(value.Length - 24, ",");
            if (value.Length >= 28)
                reval = reval.Insert(value.Length - 27, ",");
            if (value.Length >= 31)
                reval = reval.Insert(value.Length - 30, ",");
            if (value.Length >= 34)
                reval = reval.Insert(value.Length - 33, ",");
            if (value.Length >= 37)
                reval = reval.Insert(value.Length - 36, ",");
            if (value.Length >= 40)
                reval = reval.Insert(value.Length - 39, ",");
            if (value.Length >= 43)
                reval = reval.Insert(value.Length - 42, ",");

            return reval;
        }

        protected override void OnTextChanged(TextChangedEventArgs e)
        {
            base.OnTextChanged(e);

            reval = ApplyCurrencyTemplate(Text);

            if (!string.IsNullOrEmpty(reval))
                Currency = DeleteCommas(reval);
            else
                Currency = "0";

            Text = reval;
            SelectionStart = reval.Length;

        }

        private void TextBox_Loaded(object sender, RoutedEventArgs e)
        {
            if (Currency != "0")
                Text = ApplyCurrencyTemplate(Currency.ToString());
        }
    }
}
